// viewlogistics.js
var shihan = require('../../../common/fetch');
var config = require('../../../common/config');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    toast_flag: false,
    currenType: '',
    showLoading: true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this;
    that.setData({
      id: options.id,
      currenType: options.index
    })
    if (options.index==1) {
      that.setData({
        url: config.track_order
      })
    } else if (options.index == 2) {
      that.setData({
        url: config.agent_order_track
      })
    } else if (options.index == 3) {
      that.setData({
        url: config.agent_return_track
      })
    }
    that.getCourierInfo();
  },
  getCourierInfo: function() {
    var that = this;
    shihan.sendRequest(that.data.url, 'POST', { order_id: that.data.id })
      .then(function(response) {
        if (response.data.code == 0) {
          if (that.data.currenType == 1) {
            that.setData({
              courier_detail: {
                sh_product_image: response.data.data.sh_product_image,
                sh_shipment_company: response.data.data.sh_shipment_list[0].sh_shipment_company,
                sh_track_no: response.data.data.sh_shipment_list[0].sh_track_no,
                sh_shipment_list: response.data.data.sh_shipment_list[0].sh_track_list
              }
            })
          } else if (that.data.currenType == 2 || that.data.currenType == 3) {
            that.setData({
              courier_detail: {
                sh_product_image: response.data.data.sh_image,
                sh_shipment_company: response.data.data.sh_shipment_company,
                sh_track_no: response.data.data.sh_track_no,
                sh_shipment_list: response.data.data.sh_shipment_list
              }
            })
          }
        }
        that.setData({
          showLoading: false
        })
        wx.stopPullDownRefresh()
      }, function(error) {
        console.log(error);
      });
  },
  copyorder: function(e) {
    var that = this
    wx.setClipboardData({
      data: String(e.currentTarget.dataset.id),
      success: function(res) {
        wx.getClipboardData({
          success: function(res) {
            if (res.data) {
              console.log('复制成功')
            }
          }
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {
    this.setData({
      order_id: '',
      courier_id: ''
    })
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    this.getCourierInfo();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  }
})